<?php

class Mod_Index extends CI_Model
{

  public function getLatestAds($start, $num, $q)
  {
    $this->db->select('a.*')
        ->select('CONCAT(u.'.field('lname').'," ",u.'.field('fname').') AS '.field('user'), FALSE)
        ->select('c.'.field('name').' AS '.field('category'))
        ->select('t.'.field('name').' AS '.field('type'))
        ->select('l.'.field('name').' AS '.field('location'))
        ->select('i.'.field('image'))
        ->from(table('ads').' a')
        ->join(table('user').' u', 'u.'.field('id').'=a.'.field('userId'))
        ->join(table('category').' c', 'c.'.field('id').'=a.'.field('categoryId'))
        ->join(table('type').' t', 't.'.field('id').'=a.'.field('typeId'))
        ->join(table('location').' l', 'l.'.field('id').'=a.'.field('locationId'))
        ->join(table('image').' i', 'i.'.field('adsId').'=a.'.field('id').' AND i.'.field('isDefault').'=1', 'left')
        ->where('a.'.field('isDisable'), 0)
        ->where('a.'.field('deletedAt'), NULL)
        ->where('u.'.field('deletedAt'), NULL)
        ->order_by('a.'.field('createdAt'), 'DESC');

    $this->db->like('a.'.field('title'), $q);
        
    $this->db->limit($num, $start);
    return $this->db->get();
  }
  
  public function getLatestAdsNum($q)
  {
    $objDb = $this->getAllLatestAds($q);
    return $objDb->num_rows();
  }
  
  public function getAllLatestAds($q)
  {
    $this->db->select('a.*')
        ->select('CONCAT(u.'.field('lname').'," ",u.'.field('fname').') AS '.field('user'), FALSE)
        ->select('c.'.field('name').' AS '.field('category'))
        ->select('t.'.field('name').' AS '.field('type'))
        ->select('l.'.field('name').' AS '.field('location'))
        ->select('i.'.field('image'))
        ->from(table('ads').' a')
        ->join(table('user').' u', 'u.'.field('id').'=a.'.field('userId'))
        ->join(table('category').' c', 'c.'.field('id').'=a.'.field('categoryId'))
        ->join(table('type').' t', 't.'.field('id').'=a.'.field('typeId'))
        ->join(table('location').' l', 'l.'.field('id').'=a.'.field('locationId'))
        ->join(table('image').' i', 'i.'.field('adsId').'=a.'.field('id').' AND i.'.field('isDefault').'=1', 'left')
        ->where('a.'.field('isDisable'), 0)
        ->where('a.'.field('deletedAt'), NULL)
        ->where('u.'.field('deletedAt'), NULL)
        ->order_by('a.'.field('createdAt'), 'DESC');
          
    $this->db->like('a.'.field('title'), $q);

    return $this->db->get();
  }

  public function getCategory()
  {
    $this->db->select('*')
              ->from(table('category'))
              ->where(field('isDisable'), 0);

    return $this->db->get();  
  }
  
  public function getAdsById($id)
  {
    $this->db->select('a.*')
        ->select('CONCAT(u.'.field('lname').'," ",u.'.field('fname').') AS '.field('user').', u.'.field('email').', u.'.field('phone'), FALSE)
        ->select('c.'.field('name').' AS '.field('category'))
        ->select('t.'.field('name').' AS '.field('type'))
        ->select('l.'.field('name').' AS '.field('location'))
        ->select('i.'.field('image'))
        ->from(table('ads').' a')
        ->join(table('user').' u', 'u.'.field('id').'=a.'.field('userId'))
        ->join(table('category').' c', 'c.'.field('id').'=a.'.field('categoryId'))
        ->join(table('type').' t', 't.'.field('id').'=a.'.field('typeId'))
        ->join(table('location').' l', 'l.'.field('id').'=a.'.field('locationId'))
        ->join(table('image').' i', 'i.'.field('adsId').'=a.'.field('id').' AND i.'.field('isDefault').'=1', 'left')
        ->where('a.'.field('deletedAt'), NULL)
        ->where('u.'.field('deletedAt'), NULL)
        ->where('a.'.field('id'), $id);
        
    $result = $this->db->get();
    return $result->row_array();
  }
}